<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Gift as GiftModel;
use app\common\model\SelfAddress;
use app\common\model\UserAddress;
use app\common\model\Order as OrderModel;
use think\Config;

/**
 * 订单
 */
class Order extends Api
{
    /**
     * 订单详情
     */
    public function detail()
    {
        $user_id = $this->auth->id;
        $data = GiftModel::alias('g')
            ->join('user_address u', 'g.id = u.gift_id')
            ->where('u.user_id', $user_id)
            ->field('u.id,u.gift_id,g.name, g.detail_img as img, u.type, u.mobile, u.status, province_id,city_id,district_id,detail as address, self_id,u.muslim')
            ->find();
        if ($data) {
            if ($data['type'] == 1) {
                $address = SelfAddress::where('id', $data['self_id'])->field('city,address')->find();
                $data['self_city'] = $address['city'];
                $data['address'] = $address['address'];
                $data['time'] = Config::get('site.self_time_begin'). ' — '.Config::get('site.self_time_end');
            } else {
                $data['ordernum'] = OrderModel::where('user_id', $user_id)->value('ordernum as number');
                $data['time'] = Config::get('site.send_time_begin'). ' — '.Config::get('site.send_time_end');
            }
            $data['select_end'] = Config::get('site.select_end');
            $data['confirm_time'] = Config::get('site.confirm_time');
        }
        $this->success('', $data);
    }

    /**
     * 自提订单确认
     */
    public function confirm()
    {
        $user_id = $this->auth->id;
        $confirm = UserAddress::where('user_id', $user_id)->update(['status'=>1]);
        if ($confirm) {
            $this->success('');
        } else {
            $this->error('已收货');
        }

    }
}